﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Kugar.Core.IO;
using System.IO;

namespace AHJT.Web
{

    public class StringLengthValidator : BaseValidator
    {
        protected static string scriptPath = @"\Scripts\KugarValidatorScript.js";
        static StringLengthValidator()
        {
            var realPath = HttpContext.Current.Server.MapPath(@"\");

            if (FileManager.FileExists(Path.Combine(realPath, @"Scripts\KugarValidatorScript.js")))
            {
                scriptPath = @"\Scripts\KugarValidatorScript.js";
            }
            else if (FileManager.FileExists(Path.Combine(realPath, @"KugarValidatorScript.js")))
            {
                scriptPath = @"\KugarValidatorScript.js";
            }
        }

        #region Overrides of BaseValidator

        protected override bool EvaluateIsValid()
        {
            string str = string.Empty;
            string controlToValidate = this.ControlToValidate;

            if (controlToValidate.Length > 0)
            {
                str = base.GetControlValidationValue(controlToValidate).Trim();

                var max = true;
                var min = true;

                if (this.MaxLength >= 0 && str.Length > this.MaxLength)
                {
                    max = false;
                }

                if (this.MinLength >= 0 && str.Length < this.MinLength)
                {
                    min = false;
                }

                return max && min;
            }
            else
            {
                return false;
            }

        }

        #endregion

        public int MaxLength { get; set; }

        public int MinLength { set; get; }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (!this.Page.ClientScript.IsClientScriptBlockRegistered(typeof(StringLengthValidator), "KugarValidatorScript"))
            {

                this.Page.ClientScript.RegisterClientScriptInclude("KugarValidatorScript", scriptPath);
            }

        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            //if (!this.RenderUplevel)
            //    return;



            base.Page.ClientScript.RegisterExpandoAttribute(this.ID, "evaluationfunction", "StringLengthValidatorEvaluateIsValid");
            //base.Page.ClientScript.RegisterExpandoAttribute();
            base.Page.ClientScript.RegisterExpandoAttribute(this.ID, "MaxLength", MaxLength.ToString());
            base.Page.ClientScript.RegisterExpandoAttribute(this.ID, "MinLength", MinLength.ToString());
            //writer.AddAttribute("MaxLength", MaxLength.ToString());
            //writer.AddAttribute( "MinLength", MinLength.ToString());
            //writer.Flush();
            //writer.WriteLine();
            //writer.Indent++;

            //HtmlTextWriter writer1 =writer;
            //base.AddExpandoAttribute(writer1, clientId, "evaluationfunction", "RegularExpressionValidatorEvaluateIsValid", false);
            //if (this.ValidationExpression.Length <= 0)
            //    return;
            //this.AddExpandoAttribute(writer1, clientId, "validationexpression", this.ValidationExpression);
        }
    }
}